/*
 * @Author: 麦冬果果
 * @Date: 2021-09-07 11:24:57
 * @LastEditTime: 2021-09-07 11:53:09
 * @LastEditors: 麦冬果果
 * @Description: 快速接口
 * @FilePath: /svc/src/app/dao/fastApi.go
 * 可以输入预定的版权声明、个性签名、空行等
 */
package dao

import (
	"database/sql"
	"fastapi/src/app/define"
	"fastapi/src/app/service"

	"github.com/gogf/gf/database/gdb"
	"github.com/gogf/gf/errors/gerror"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/util/gconv"
)

var FastApi = fastApi{}

type fastApi struct{}

//获取单条数据
func (d *fastApi) GetOne(appApi *define.AppApi, params g.Map) (result gdb.Record, err error) {
	if appApi == nil {
		err = gerror.New("接口数据不能为空")
		return nil, err
	}
	m, err := service.FastApi.GetFindModel(appApi, params)
	if err != nil {
		return nil, err
	}
	return m.One() //OmitEmpty().
}

//获取所有数据
func (d *fastApi) GetAll(appApi *define.AppApi, params g.Map) (gdb.Result, error) {
	m, err := service.FastApi.GetFindModel(appApi, params)
	if err != nil {
		return nil, err
	}
	return m.All()
}

//分页查询 GetPage
func (d *fastApi) GetPage(appApi *define.AppApi, params g.Map) (total int, page int, list gdb.Result, err error) {

	var m *gdb.Model
	if appApi == nil {
		err = gerror.New("接口数据不能为空")
		return
	}
	m = g.DB("default").Model(appApi.TableName).Safe()

	//查询是否是多表关联
	tableJoins := gconv.SliceMapDeep(appApi.TableJoin)
	if len(tableJoins) != 0 {
		m, err = service.FastApi.ModelJoin(tableJoins, m)
		if err != nil {
			return
		}
	}

	//where 查询
	tableWhere := gconv.SliceMapDeep(appApi.WhereBy)
	if len(tableWhere) != 0 {
		m, err = service.FastApi.ModelWhere(tableWhere, m, params)
		if err != nil {
			return
		}
	}

	// 查询总记录数(总行数)
	total, err = m.Count()
	if err != nil {
		err = gerror.New("获取总记录数失败")
		return
	}
	//order
	tableOrder := gconv.SliceMapDeep(appApi.OrderBy)
	if len(tableOrder) != 0 {
		m, err = service.FastApi.ModelOrder(tableOrder, m)
		if err != nil {
			return
		}
	}

	//group
	tableGroup := gconv.SliceMapDeep(appApi.GroupBy)
	if len(tableGroup) != 0 {
		m, err = service.FastApi.ModelGroup(tableGroup, m)
		if err != nil {
			return
		}
	}

	//fields
	tableField := gconv.SliceMapDeep(appApi.FieldBy)
	if len(tableField) != 0 {
		m, err = service.FastApi.ModelField(tableField, m)
		if err != nil {
			return
		}
	}
	page = 1
	if gconv.Int(params["pageNum"]) != 0 {
		page = gconv.Int(params["pageNum"])
	}
	size := define.AdminPageNum
	if gconv.Int(params["pageSize"]) != 0 {
		size = gconv.Int(params["pageSize"])
	}
	// 分页排序查询
	m = m.Page(page, size)
	list, err = m.All()
	return
}

//新增数据
func (d *fastApi) Add(appApi *define.AppApi, params g.Map) (result sql.Result, err error) {
	if appApi == nil {
		err = gerror.New("接口数据不能为空")
		return
	}
	if g.IsEmpty(params) {
		err = gerror.New("新增数据不能为空")
		return
	}

	m, err := service.FastApi.GetEditModel(appApi, params)

	if err != nil {
		return nil, err
	}
	return m.Insert()
}

//修改数据
func (d *fastApi) Edit(appApi *define.AppApi, params g.Map) (err error) {
	if appApi == nil {
		err = gerror.New("接口数据不能为空")
		return
	}
	if g.IsEmpty(params) {
		err = gerror.New("修改数据不能为空")
		return
	}

	m := g.DB("default").Model(appApi.TableName).Safe()
	//查询是否是多表关联
	tableJoins := gconv.SliceMapDeep(appApi.TableJoin)
	if len(tableJoins) != 0 {
		m, err = service.FastApi.ModelJoin(tableJoins, m)
	}

	//where 查询
	tableWhere := gconv.SliceMapDeep(appApi.WhereBy)
	if len(tableWhere) != 0 {
		m, err = service.FastApi.ModelWhere(tableWhere, m, params)
	}
	//data
	tableField := gconv.SliceMapDeep(appApi.FieldBy)
	if len(tableField) != 0 {
		m, err = service.FastApi.ModelData(tableField, m, params)
	}

	if err != nil {
		return err
	}
	_, err = m.Update() //m.Update()
	return
}

//删除数据 Delete
func (d *fastApi) Delete(appApi *define.AppApi, params g.Map) (err error) {
	if appApi == nil {
		err = gerror.New("接口数据不能为空")
		return
	}
	if g.IsEmpty(params) {
		err = gerror.New("参数不能为空")
		return
	}

	m := g.DB("default").Model(appApi.TableName).Safe()
	//where 查询
	tableWhere := gconv.SliceMapDeep(appApi.WhereBy)
	if len(tableWhere) != 0 {
		m, err = service.FastApi.ModelWhere(tableWhere, m, params)
	}
	if err != nil {
		return err
	}
	_, err = m.Delete()
	return
}
